package com.qqtech.qquan.circle.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.exception.BizzException;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.enums.CircleConst;
import com.qqtech.qquan.circle.enums.CircleUpgradeApplyStatusDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.model.CircleUpgradeApply;
import com.qqtech.qquan.circle.model.CircleUpgradeApplyQuery;
import com.qqtech.qquan.circle.model.CircleUpgradeApplyVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleSysDictService;
import com.qqtech.qquan.circle.service.CircleUpgradeApplyService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.sys.model.SysAreaVo;
import com.qqtech.qquan.sys.service.SysAreaService;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/upgrade/apply")
public class CircleUpgradeApplyController extends BaseWebController<CircleUpgradeApply, CircleUpgradeApplyQuery> {
	@Resource
	private CircleUpgradeApplyService circleUpgradeApplyService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleSysDictService circleSysDictService;
	@Resource
	private CommunityBaseService communityBaseService;
	@Resource
	private SysAreaService  sysAreaService;
	@Resource
	private UserBaseService userBaseService;
	
	@Override
	protected BaseService<CircleUpgradeApply> getBaseService() {
		return circleUpgradeApplyService;
	}
	
	/**
	 * 申请升级到平台圈子
	 * @param apply
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/apply2Upgrade")
	@ResponseBody
	public Result apply2Upgrade(@RequestBody CircleUpgradeApply apply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer circleId = apply.getCircleId();
		if(circleId == null){
			throw new BizzException("圈子id信息为空.");
		}
		apply.setApplyId(curUserId);
		CircleBaseVo circle = circleBaseService.getById(circleId);
		if(circle==null){
			throw new BizzException("找不到这个圈子.");
		}
		apply.setFromZoneLevel(circle.getZoneLevel());//升级前-区域范围
		CircleUpgradeApply applyQ = new CircleUpgradeApply();
		  applyQ.setCircleId(circleId);
		  applyQ.setStatus(CircleUpgradeApplyStatusDictEnum.TODO.getCode());
		  int count = circleUpgradeApplyService.queryCount(applyQ);
		  if(count>0){
			  throw new BizzException("您已提交了这个圈子的升级申请,不能重复提交.");
		  }
		circleUpgradeApplyService.addApply(apply);
		return result;
	}
	
	/**
	 * 取升级申请
	 * @param apply
	 * @return
	 * @throws BizzException
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/cancel")
	@ResponseBody
	public Result cancel(@RequestBody CircleUpgradeApply apply) throws BizzException {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		Integer id = apply.getId();
		if(id == null){
			throw new BizzException("申请id信息为空.");
		}
		CircleUpgradeApply applyQ = circleUpgradeApplyService.getById(id);
		if(applyQ == null){
			throw new BizzException("找不申请记录.");
		}
		if(applyQ.getStatus() != CircleUpgradeApplyStatusDictEnum.TODO.getCode()){
			throw new BizzException("申请已处理,不能取消!");
		}
		if(!curUserId.equals(applyQ.getApplyId())){
			throw new BizzException("你没有权限操作!");
		}
		circleUpgradeApplyService.deleteById(id);
		return result;
	}
	
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/myApplys")
	@ResponseBody
	public Result myApplys() {
		Result result = new Result();
		Integer curUserId = super.getUserId();// 当前用户id
		CircleUpgradeApply applyQ = new CircleUpgradeApply();
		applyQ.setApplyId(curUserId);
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "id"));
		List<CircleUpgradeApplyVo> applyList = circleUpgradeApplyService.queryList(applyQ,pageKit);
		if(applyList!=null && applyList.size()>0){
			CircleBaseVo circle = null;
			for(CircleUpgradeApplyVo apply: applyList){
				circle = circleBaseService.getById(apply.getCircleId());
				if(circle!=null){
					apply.setCircleName(circle.getName());
					apply.setCircleLogo(circle.getLogo());
				}
				apply.setFromZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(apply.getFromZoneLevel())));
				apply.setToZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(apply.getToZoneLevel())));
			}
		}
		result.put("applyList", applyList);
		return result;

	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody CircleUpgradeApply apply) {
		Result result = new Result();
		Integer id = apply.getId();// 前端-参数
		if (id == null) {
			result.addErr("申请Id信息为空！");
			return result;
		}
		CircleUpgradeApplyVo applyVo = circleUpgradeApplyService.getById(id);
		UserBaseVo userBaseVo = userBaseService.getById(applyVo.getApplyId());
		if(userBaseVo!=null){
			applyVo.setApplyName(userBaseVo.getNick());
		}
		CircleBaseVo circleVo = circleBaseService.getById(applyVo.getCircleId());
		if(circleVo!=null){
			applyVo.setCircleName(circleVo.getName());
			applyVo.setCircleLogo(circleVo.getLogo());
			CommunityBaseVo communityVo = communityBaseService.getById(circleVo.getCommunityId());
			if(communityVo!=null){
				applyVo.setCommunityName(communityVo.getName());
			}
		}
		applyVo.setFromZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(applyVo.getFromZoneLevel())));
		applyVo.setToZoneLevelName(circleSysDictService.dictValue(CircleConst.DICTYPE_CIRCLE_ZONE_LEVEL, String.valueOf(applyVo.getToZoneLevel())));
		if(applyVo.getToCityId()!=null){
			SysAreaVo area = sysAreaService.getById(applyVo.getToCityId());
			if(area!=null){
				applyVo.setToCityName(area.getName());
			}
		}
		String lastToExtCommunityIds = circleVo.getExtCommunityIds();//已扩展社区ids
		if(StringUtil.isNotBlank(lastToExtCommunityIds)){
			String[] strCommunityIds = lastToExtCommunityIds.split(";");
			List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
			if(!CommunityIds.isEmpty()){
				CommunityBaseQuery communityQ = new CommunityBaseQuery();
				communityQ.setIncludeIds(CommunityIds);
				List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
				applyVo.setLastToExtCommunityNames(getCommunityNames(communityList));
			}
		}else{
			applyVo.setLastToExtCommunityNames("未扩展");
		}
		String toExtCommunityIds = applyVo.getToExtCommunityIds();//本次扩展社区ids
		if(StringUtil.isNotBlank(toExtCommunityIds)){
			String[] strCommunityIds = toExtCommunityIds.split(";");
			List<Integer> CommunityIds = getIntegerCommunityIds(strCommunityIds);
			if(!CommunityIds.isEmpty()){
				CommunityBaseQuery communityQ = new CommunityBaseQuery();
				communityQ.setIncludeIds(CommunityIds);
				List<CommunityBaseVo> communityList = communityBaseService.queryList(communityQ);
				applyVo.setToExtCommunityNames(getCommunityNames(communityList));
			}
		}
		if(applyVo.getToCityId()!=null){
			SysAreaVo area = sysAreaService.getById(applyVo.getToCityId());
			if(area!=null){
				applyVo.setToCityName(area.getName());
			}
		}else{
			applyVo.setToCityName("未扩展");
		}
		
		result.put("applyVo", applyVo);
		return result;
	}
	
	/**
	 * 返回社区字符串【以、号隔开】
	 * @param communityList
	 * @return
	 */
	private String getCommunityNames(List<CommunityBaseVo> communityList){
		String names = "";
		if(communityList!=null && communityList.size() >0){
			for(CommunityBaseVo community : communityList){
				names += community.getName() +"、";
			}
		}
		if(StringUtil.isNotBlank(names)){
			names = names.substring(0, names.length()-1);
		}
		return names;
	}
	/**
	 * List<String>转换为List<Integer>
	 * @param strCommunityIds
	 * @return
	 */
	private List<Integer> getIntegerCommunityIds(String[] strCommunityIds){
		List<Integer> communityIds = Lists.newArrayList();
		if(strCommunityIds!=null && strCommunityIds.length >0){
			for(String strCommunityId : strCommunityIds){
				communityIds.add(Integer.parseInt(strCommunityId));
			}
		}
		
		return communityIds;
	}
}
